package net.skyaims.test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created by wujianing on 14-3-10.
 */
public class MyClient implements Runnable {

    public static void main(String[] args) {

        int accpet = 30;

        Executor e = Executors.newFixedThreadPool(accpet);

        for (int i = 0; i < accpet; i++) {
            e.execute(new MyClient());
        }

    }

    public void keepAlive() throws IOException {
        Socket socket = new Socket();
        try {
            socket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 8081));
        } catch (IOException e) {
            e.printStackTrace();
        }
        int time = 2000;
        int step = 400;
        while (true) {
            try {
                socket.getOutputStream().write(new byte[]{0, 0, 0, 0, 0});
                Thread.sleep((time += step));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void run() {
        try {
            keepAlive();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
